package com.javaoopfour;

import com.javaoopthree.Cart;

import java.util.Scanner;

public class Menus {
    int mId;
    String mName;
    double mPrice;
    int mCount; //库存
    int count;//点餐数量


    public Menus(){

    }

    public Menus(int mId, String mName, double mPrice, int mCount, int count) {
        this.mId = mId;
        this.mName = mName;
        this.mPrice = mPrice;
        this.mCount = mCount;
        this.count = count;
    }

    //初始化菜品
   public Menus[] initMenu(Menus[] menus){
      Menus menus1 = new Menus(1,"红烧肉",15.6,10,0);
      Menus menus2 = new Menus(2,"酸菜鱼",25,10,0);
      Menus menus3 = new Menus(3,"口水鸡",13,10,0);
      Menus menus4 = new Menus(4,"牛肉面",10,10,0);
      Menus menus5 = new Menus(5,"西湖醋鱼",56,10,0);
       Menus menus6 = new Menus(6,"肉末茄子",15,10,0);
       Menus menus7 = new Menus(7,"麻婆豆腐",7,10,0);
       Menus menus8 = new Menus(8,"地三鲜",13,10,0);
       menus[0]=menus1;
       menus[1]=menus2;
       menus[2]=menus3;
       menus[3]=menus4;
       menus[4]=menus5;
       menus[5]=menus6;
       menus[6]=menus7;
       menus[7]=menus8;
       return  menus;
   }

    public void showOrder(Menus[] menus){
        System.out.println("菜品编号\t\t\t菜品名称\t\t\t菜品价格\t\t\t点餐数量");
        for (int i = 0; i <menus.length ; i++) {
            if(menus[i] !=null){
                System.out.println(menus[i].mId+"\t\t\t"+menus[i].mName+"\t\t\t"+menus[i].mPrice+"\t\t\t"+menus[i].count);
            }
        }
    }

   //展示菜单
    public void showMenus(Menus[] menus){
        System.out.println("菜品编号\t\t\t菜品名称\t\t\t菜品价格\t\t\t库存");
        for (int i = 0; i <menus.length ; i++) {
            if(menus[i] !=null){
                System.out.println(menus[i].mId+"\t\t\t"+menus[i].mName+"\t\t\t"+menus[i].mPrice+"\t\t\t"+menus[i].mCount);
            }
        }
    }

    //点餐//
     public boolean addCart(Cart cart, Menus[] cartMenus, Menus[] menus){
         boolean isOk=true;
         Scanner scanner = new Scanner(System.in);
         System.out.println("请选择菜品编号：");
         int mid = scanner.nextInt();
         System.out.println("请输入点餐数量");
         int  num = scanner.nextInt();
         //根据编号去菜单查询
         Menus menu = getMenuById(mid,menus);
         if(menu ==null){
             System.out.println("菜品编号不存在");
             return  false;
         }
         //判断是老菜还是新菜
         int index = checkMenusOld(cartMenus, menu);
         if(index>=0){//老菜
             cartMenus[index].count+=num;
         }else {
             menu.count+=num;
             //加入零临时的容器
             addNewsMenu(cartMenus, menu);
         }
         //把零临时的容器添加到餐袋
         //cart.menus=cartMenus;


         System.out.println("点餐成功...");
         System.out.println("是否继续点餐(y/n)");
         String next = scanner.next();
         if(next.equals("n")){
             isOk=false;
        }
         return isOk;
     }


     //根据菜品id进行查询
    public Menus getMenuById(int mid,Menus[] menus){
        Menus menus1=null;
        for (int i = 0; i <menus.length ; i++) {
            if(menus[i]!=null && menus[i].mId==mid){
                menus1=menus[i];
                break;
            }
        }
        return menus1;

    }

    //查询老菜
    public int checkMenusOld(Menus[] cartMenus, Menus menu ){
        int index=-1;
        for (int i = 0; i <cartMenus.length ; i++) {
            if(cartMenus[i]!=null && cartMenus[i].mId==menu.mId){
                index=i;
                break;
            }
        }
        return index;
    }


    //将新菜放入临时的容器
    public void addNewsMenu(Menus[] cartMenus,Menus menu ){
        for (int i = 0; i < cartMenus.length; i++) {
            if(cartMenus[i] ==null){
                cartMenus[i]=menu;
                break;
            }
        }
    }

    //从购物车删除菜品
    public void delCartMenus(Cart cart, Menus menus){
        for (int i = 0; i < cart.menus.length; i++) {
            /*if(cart.menus[i] !=null && cart.menus[i].mId==menus.mId){
                cart.menus[i]=null;
                break;
            }*/
        }

    }


}
